import {
    validate
} from './validate';
import {
    submit
} from './submit';
import {
    editView
} from './views/edit.view';
import{
    msg, modal
}from '../common/layer';

const editBtnHandler = () => {

    let selectedList = $('#table').bootstrapTable('getSelections');
    if (selectedList.length === 0) {
        msg('请选择一项编辑');
        return;
    }

    const localData = selectedList[0];
    modal('编辑', editView(localData));

    $(".modal-form input").iCheck({
        checkboxClass: 'icheckbox_minimal-blue',
        radioClass: 'iradio_minimal-blue'
    });
    let  $checked = $('input:input[name="is_copy"]:checked').val();
    if (parseInt($checked)===2) {
        $('#copy_email,#send_name').hide();
    }
    $('.is_copy').on('ifChecked', function(event){
        let val = $('input:input[name="is_copy"]:checked').val();
        if(parseInt(val) === 1){
            $('#copy_email,#send_name').show();
        }else{
            $('#copy_email,#send_name').hide();
        }
    });

    $('#submitBtn').click(() => {
        let data = {};
        $.each($('#form').serializeArray(), (key, val) => {
            data[val.name] = val.value;
        });
        if (!validate(data)) {
            return;
        }
        submit(GLOBAL_EditUrl, data);
    });

};

export {
    editBtnHandler
}